import Vue from 'vue'
import App from './App.vue'
import router from './router/index'
import {Toast, Notify} from 'vant'
import 'lib-flexible/flexible'
import utils from '@/utils/utils.js'
import api from './api/index'
import WxSdk from './lib/wx-sdk'
import '@/style/rest.scss'
import './style/base.scss'
import '../../filters/register-global'
import './components/register-global'
import '@/components/register-global'
import 'swiper/dist/css/swiper.css'
import VConsole from 'vconsole'

// new VConsole()
if (window.__wxjs_environment === 'miniprogram') {
    if (utils.getUrlParam('miniToken')) {
        utils.store.set('miniAppToken', utils.getUrlParam('miniToken'))
    } else {
        utils.store.set('miniAppToken', '')
    }
}
const wxSdkInit = () => {
    return new Promise((resolve, reject) => {
        if (!utils.browser.isWx) {
            reject('不是微信环境')
        }
        const wx = new WxSdk({
            success: res => {
                window.$wx = wx.$wx()
                window.$wxMini = wx.$wxMini()
                resolve(wx)
            },
            fail: e => {
                reject(e)
            }
        })
    })
}
Vue.use(Toast)
Vue.config.productionTip = false
Vue.prototype.$toast = Toast
Vue.prototype.$notify = Notify
Vue.prototype.$utils = utils
Vue.prototype.$api = api
Vue.prototype.$wxSdkInit = wxSdkInit

;(async () => {
    Toast.loading({
        duration: 0,
        message: '加载中'
    })
    if (utils.browser.isWx) {
        try {
            await wxSdkInit()
        } catch (e) {
            console.error(e)
        }
    }
    new Vue({
        router,
        data: {
            userInfo: null,
            isMiniApp: window.__wxjs_environment === 'miniprogram'
        },
        computed: {
            origin() {
                if (process.env.NODE_ENV !== 'production') {
                    return 'https://platform.huankeyanxue.com'
                } else {
                    return 'https://platform.huankeyanxue.com'
                }
            }
        },
        render: h => h(App),
        created() {
            // this.getUserInfo()
        },
        mounted() {
            Toast.clear()
            const ready = () => {
                this.isMiniApp = (window.__wxjs_environment === 'miniprogram')
            }
            if (!window.WeixinJSBridge || !WeixinJSBridge.invoke) {
                document.addEventListener('WeixinJSBridgeReady', ready, false)
            } else {
                ready()
            }
        },
        methods: {
            async getUserInfo() {
                try {
                    let {data} = await api.user.info()
                    utils.store.set('userInfo', data)
                    this.userInfo = data
                    return data
                } catch (e) {
                    utils.store.remove('userInfo')
                    this.userInfo = null
                    return false
                }
            },
        }
    }).$mount('#app')
})()

